package com.excel.qlexpress;



import com.corpgovernment.common.constant.RegularExpressionConst;
import com.corpgovernment.common.enums.GenderEnum;
import com.corpgovernment.common.enums.NationalityEnum;
import com.corpgovernment.common.enums.PhoneCountryCodeEnum;
import com.corpgovernment.common.utils.DateUtil;
import com.ctrip.corp.obt.generic.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.*;
import java.util.function.Function;
import java.util.regex.Pattern;
import java.util.stream.Collectors;


@Slf4j
public class EmployQlFunction {
    //校验国籍二字码
    public static boolean checkEmployNationality(String nationality){
        if (StringUtils.isNotBlank(nationality) && Objects.isNull(NationalityEnum.getByCode(nationality))) {
            return true;
        }
        return false;
    }
    //校验性别
    public static boolean checkEmployGender(String gender){
        if (StringUtils.isNotBlank(gender) && Objects.isNull(GenderEnum.getByCode(gender))) {
            return true;
        }
        return false;
    }
    //校验国家码
    public static boolean checkEmployCountryCode(String countryCode){
        if (StringUtils.isNotBlank(countryCode) && Objects.isNull(PhoneCountryCodeEnum.getByCountryCode(countryCode))) {
            return true;
        }
        return false;
    }
    //校验 国家码+手机号
    public static boolean checkEmployMobilePhone(String countryCode, String mobilePhone){
        if(StringUtils.isBlank(mobilePhone)){
            return false;
        }
        Pattern p = Pattern.compile(RegularExpressionConst.PHONE);
        if ((StringUtils.isBlank(countryCode) || PhoneCountryCodeEnum.CN.getCountryCode().equals(countryCode)) &&
                (StringUtils.isBlank(mobilePhone) || mobilePhone.length() > 11 ||  !p.matcher(mobilePhone).find()) ) {
            return true;
        }
        return false;
    }
    //校验 邮箱格式
    public static boolean checkEmployEmail(String email){
        if (!StringUtils.isEmpty(email) && !email.matches(RegularExpressionConst.MAIL)) {
            return true;
        }
        return false;
    }
    //校验 生日格式
    public static boolean checkEmployBirth(String birthDayString){
        if (StringUtils.isNotBlank(birthDayString)) {
            Date birth = DateUtil.stringToDate(birthDayString, DateUtil.DF_YMD);
            if (birth == null) {
                return true;
            }
        }
        return false;
    }
    //校验 excel重复数据 动态字段
    public static <T> List<T> getDuplicateEmployField(List<T> list, Function<T, String> fieldMapper){
        Map<String, Long> countMap = list.stream().collect(Collectors.groupingBy(fieldMapper, Collectors.counting()));
        List<T> duplicates = list.stream()
                .filter(e -> countMap.get(fieldMapper.apply(e))>1)
                .collect(Collectors.toList());
        return duplicates;
    }


}